package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.entity.Register;
import where2go.sctu.com.zhiyou.entity.VerificationCode;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.activity
 * Created by 张某人
 * CreatedTime:on 2018/5/25   12:43
 * Description: 注册
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etVerify;
    private EditText enterVerify;
    private EditText etPassword;
    private Button btnVerify;
    private Button btnSubmit;
    private ImageView btnBack;

    private VerificationCode verificationCode = new VerificationCode();
    private Register register=new Register();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        etPassword = (EditText) findViewById(R.id.et_enter_password);
        etVerify = (EditText) findViewById(R.id.et_verify);
        enterVerify = (EditText) findViewById(R.id.et_enter_verify);

        btnBack = (ImageView) findViewById(R.id.register_back);
        btnBack.setOnClickListener(this);

        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(this);

        btnVerify = (Button) findViewById(R.id.btn_verify);
        btnVerify.setOnClickListener(this);
    }

    /**
     * 按钮点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_verify:
                String email=etVerify.getText().toString().trim();
                String url = "http://47.104.80.155/youyiyou/users/getVerificationCode?email=";
                getVerificationCode(url+email);
                break;
            case R.id.btn_submit:
                registerEmail();
                break;
            case R.id.register_back:
                finish();
                break;
        }
    }

    /**
     * 发送验证码至邮箱
     * @param url
     */
    private void getVerificationCode(String url) {
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<VerificationCode>() {
                }.getType();
                //判断错误码
                verificationCode=gson.fromJson(response,type);
                int error=verificationCode.getError();
                if (error==0){
                    Toast.makeText(RegisterActivity.this,"验证码发送成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(RegisterActivity.this,"你还没输入邮箱哟", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        request.setTag("getVerificationCode");
        MyApplication.getHttpQueues().add(request);
    }

    /**
     * 邮箱注册
     */
    private void registerEmail() {
        String url="http://47.104.80.155/youyiyou/users/register/email?";
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<Register>() {
                }.getType();
                //判断错误码
                register=gson.fromJson(response,type);
                int error=register.getError();
                if (error==0){
                    Toast.makeText(RegisterActivity.this,"注册成功", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
                }else if (error==109||error==110||error==111||error==112||error==113||error==114){
                    Toast.makeText(RegisterActivity.this,register.getMsg(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<String,String>();
                map.put("email",etVerify.getText().toString().trim());
                map.put("password",etPassword.getText().toString().trim());
                map.put("verificationCode",enterVerify.getText().toString().trim());
                return map;
            }
        };
        request.setTag("registerPost");
        MyApplication.getHttpQueues().add(request);
    }
}
